import cv2
import numpy as np

# 1. 读取原图和模板图
img = cv2.imread("../images/ctrl.png")
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

temp = cv2.imread("../images/basketball.png")
temp_gray = cv2.cvtColor(temp, cv2.COLOR_BGR2GRAY)

# 2. 模板匹配
ret = cv2.matchTemplate(
    img_gray,   # 待匹配的图片
    temp_gray,  # 模板图片
    cv2.TM_CCOEFF_NORMED  # 匹配方法 (归一化相关系数法)
)
print(ret.shape)
loc = np.where(ret >= 0.8)
print(loc)   # 第一个数组是 h（y）的坐标值，第二个数组是 w（x）的坐标值
h, w, _ = temp.shape
for pt in zip(*loc[::-1]):
    cv2.rectangle(img, pt, (pt[0] + w, pt[1] + h), (0, 0, 255), 2)

cv2.imshow("img", img)
cv2.waitKey(0)
"""
a = [1, 2, 3]
b = [4, 5, 6]
print(zip(a, b))
for x,y in zip(a, b):
    print((x,y))
<zip object at 0x00000217C5ACF5C0>
(1, 4)
(2, 5)
(3, 6)
"""
"""
print(enumerate(a))
for x in enumerate(a):
    print(x)
<enumerate object at 0x000001EE10EFF5C0>
(0, 1)
(1, 2)
(2, 3)
"""